package com.hibernate.hrm.service.login;

import com.hibernate.hrm.entity.User;
import com.hibernate.hrm.dao.UserRepository;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private final UserRepository userRepository;
    private final UserService userService;
    private final OperateLogService operateLogService;
    private final HttpServletRequest request;

    public UserDetailsServiceImpl(UserRepository userRepository,
                                  UserService userService,
                                  OperateLogService operateLogService,
                                  HttpServletRequest request) {
        this.userRepository = userRepository;
        this.userService = userService;
        this.operateLogService = operateLogService;
        this.request = request;
    }

    @Override
    @Transactional
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 只查询用户，不记录日志
        return userRepository.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException("用户不存在: " + username));
    }
}